package timeitj.faces;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.event.ActionEvent;
import timeitj.ejb.SessionFacade;
import timeitj.persistence.Participance;
import timeitj.persistence.Project;

/**
 *
 * @author basti
 */
public class ProjectController {
    
    private Project project;
    
    private HtmlDataTable dataTable;
    
    private int affectedRowIndex;
    
    @EJB
    private SessionFacade sessionFacade;
    
    public ProjectController() throws Exception{
        project = new Project();
    }
    
    /**
     * Creates a new project with the current logged in user as owner.
     */
    public String createProject() {
        sessionFacade.createProject(this.project);
        project = new Project();
        return "mainPage";
    }
    
    /*
     * Deletes a project ownerd by the currently logged in user.
     */
    public void deleteProject(ActionEvent e) {        
        Project p = (Project) dataTable.getRowData();
        sessionFacade.deleteProject(p);
    }
    
    /**
     * Gets the projects of the currently logged in user.
     * @return Returns a list of timeitj.persistence.Project as the projects
     * of the currently logged in user as owner
     */
    public List<Project> getOwnedProjects() {
        // TODO: getOwnedProjects() überflüssig?
        return sessionFacade.getCurrentUser().getOwnedProjects();
    }
    
    /**
     * Gets the number of owned projects by the current user
     * @return Returns the number of owned projects as integer.
     */
    public int getOwnedProjectsCount() {
        return getOwnedProjects().size();
    }
    
    public void setProject(ActionEvent e) {
        this.project = (Project) dataTable.getRowData();
    }    
    
    /**
     * Gets the project.
     * @return Returns the project as instance of timeitj.persistence.Project
     */
    public Project getProject() {
        return project;
    }       
    
    public HtmlDataTable getDataTable() {
        return dataTable;
    }
    
    public void setDataTable(HtmlDataTable dataTable) {
        this.dataTable = dataTable;
    }
    
    public int getAffectedRowIndex() {
        return affectedRowIndex;
    }
    
    public void setAffectedRowIndex(int affectedRowIndex) {
        this.affectedRowIndex = affectedRowIndex;
    }
}
